package com.huatai.exchange.impl;

import com.faneui.common.exception.BusinessException;
import com.faneui.common.utils.DateFormatUtils;
import com.google.common.collect.Maps;
import com.huatai.exchange.dal.dao.*;
import com.huatai.exchange.dal.model.*;
import com.huatai.exchange.facade.CommunicationContents;
import com.huatai.exchange.facade.Contents;
import com.huatai.exchange.integration.base.service.CommunicationService;
import com.huatai.exchange.integration.base.service.DictService;
import com.huatai.exchange.integration.base.service.FileOperateService;
import com.huatai.exchange.send.FileSendFactory;
import com.huatai.exchange.service.ExcChannelInfoService;
import com.huatai.exchange.service.ExcDataFileInfoService;
import com.huatai.exchange.service.FileCreateCenterService;
import com.huatai.exchange.utils.VelocityContextHelper;
import com.huatai.exchange.utils.VelocityUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class ExcChannelInfoServiceImpl implements ExcChannelInfoService {

    private static final Logger logger = LoggerFactory.getLogger(ExcChannelInfoService.class);

    @Autowired
    private ExcExchangeInfoDAO exchangeInfoDAO;

    @Autowired
    private ExcCommunicationDAO excCommucationDAO;

    @Autowired
    private ExcCustomerDAO customerDAO;

    @Autowired
    private ExcDataFileInfoService excDataFileInfoService;

    @Autowired
    private VelocityContextHelper velocityHelper;


    @Autowired
    private FileSendFactory fileSendFactory;


    @Override
    public void executeExchangeDataFile(String exchangeInfoId, String occurDate) throws BusinessException {
        try {
            ExcExchangeInfo exchangeInfo = exchangeInfoDAO.queryById(exchangeInfoId);
            ExcCustomer customer = customerDAO.queryById(exchangeInfo.getCustId());

            // 上下文 信息
            Map<String, Object> resvMap = Maps.newHashMap();
            resvMap.put(Contents.FIELD_YYYYMMDD, occurDate);

            excDataFileInfoService.getFileList(exchangeInfoId, resvMap);

            logger.info("完成文件准备："+ DateFormatUtils.HHmmss.format(new Date()));
            Velocity.init();
            VelocityContext context = velocityHelper.fillContext(resvMap);
            String velocityContent = VelocityUtils.evaluate(context, exchangeInfo.getContent());

            if (!Contents.boolean_true.equals(velocityContent)) {
                throw new BusinessException(velocityContent);
            }

            // 多种传输方式
            List<ExcCommunication> coms = excCommucationDAO.queryByExchangeInfoId(exchangeInfoId);
            for (ExcCommunication com : coms) {
                fileSendFactory.getSendService(com.getCommunicationType())
                        .sendMessage(customer,resvMap);
            }

        } catch (Exception ex) {
            BusinessException.throwBusinessException(ex);
        }
    }


}